﻿using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace GPToolkit.CSV
{
	public class CSVGenerator : ICSVGenerator
	{
		#region private fields
		private ICollection<CSVRow> rows;
		private string separator;
		#endregion

		#region constructors
		/// <summary>
		/// Initialize a new instance of the CSVGenerator class
		/// </summary>
		/// <param name="rows">Collection CSV rows</param>
		/// <param name="separator">CSV separator</param>
		public CSVGenerator(ICollection<CSVRow> rows, string separator) {
			this.rows = rows;
			this.separator = separator;
		}
		#endregion

		#region ICSVGenerator Membres
		/// <summary>
		/// Get the CSV
		/// </summary>
		/// <returns>A string that represents the CSV content</returns>
		public string GetCSV() {
			var sb = new StringBuilder();

			// values rows
			foreach (CSVRow row in this.rows) {
				foreach (string value in row.Values) {
					sb.AppendFormat(CultureInfo.CurrentCulture, "{0}{1}", value, this.separator);
				}

				sb.AppendLine();
			}

			return sb.ToString();
		}

		/// <summary>
		/// Get the rows count
		/// </summary>
		public int RowCount {
			get {
				return this.rows.Count;
			}
		}

		#endregion
	}
}
